package org.dcits.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dcits.model.InTransflow;
import org.dcits.model.Useracct;
import org.dcits.service.TransferService;
import org.dcits.util.RCodeDict;
import org.dcits.util.ObjectToTransflow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("transfer")
public class TransferController {
	
	@Autowired
	private TransferService tranflowService;
	
	
	/**
	 * 通过账户编号查询客户名称
	 * @param acctNo
	 * @return
	 */
	@RequestMapping("findUsername")
	@ResponseBody
	public Map<String, Object> checkAcct(String acctNo) {
		if(isEmpry(acctNo)) {
			return outResult(RCodeDict.PARMETER_ERROR, "未输入查询账户", null);
		}
		String name = tranflowService.findUsernameByAcctNo(acctNo);
		if(isEmpry(name)) {
			return outResult(RCodeDict.DATA_NOT_FOUND, "未查询到数据", acctNo);
		}
		return outResult(RCodeDict.SUCCESS, "数据查找完成", name);
	}
	
	/**
	 * 转账接口
	 * @param inTransflow
	 * @return
	 */
	@RequestMapping("action")
	@ResponseBody
	public Map<String, Object> tran(InTransflow inTransflow) {

		// 验证参数是否输入
		if(inTransflow == null) {
			return outResult(RCodeDict.PARMETER_ERROR, "参数不能为空", null);
		}
		if(isEmpry(inTransflow.getaAcctNo())) {
			return outResult(RCodeDict.PARMETER_ERROR, "转账者账号不能为空", null);
		}
		if(isEmpry(inTransflow.getbAcctNo())) {
			return outResult(RCodeDict.PARMETER_ERROR, "被转账者账号不能为空", null);
		}
		if(isEnpry(inTransflow.getSalary())) {
			return outResult(RCodeDict.PARMETER_ERROR, "请输入转账金额", null);
		}
		if(isEmpry(inTransflow.getPassword())) {
			return outResult(RCodeDict.PARMETER_ERROR, "请输入密码", null);
		}
		
		// 校验参数
		if(inTransflow.getaAcctNo().equals(inTransflow.getbAcctNo())) {
			return outResult(RCodeDict.PARMETER_ERROR, "转账账号与被转帐账号不能相同.", null);
		}
		if(inTransflow.getSalary() <= 0) {
			return outResult(RCodeDict.PARMETER_ERROR, "转账金额不能小于或者等于 0 .", null);
		}
		Useracct aAcct = tranflowService.findUseracctByAcctNo(inTransflow.getaAcctNo());
		if(aAcct == null) {
			return outResult(RCodeDict.DATA_NOT_FOUND, "无法找到转账者账号数据.", inTransflow.getaAcctNo());
		}
		Useracct bAcct = tranflowService.findUseracctByAcctNo(inTransflow.getbAcctNo());
		if(bAcct == null) {
			return outResult(RCodeDict.DATA_NOT_FOUND, "无法找到被转账者账号数据.", inTransflow.getbAcctNo());
		}
		
		
		// 验证密码流程
		String pcode = tranflowService.checkPassword(inTransflow.getaAcctNo(), inTransflow.getPassword());
		if(RCodeDict.PASSWORD_CORRECT.equals(pcode)) {
			//密码正确
		}else if(RCodeDict.PASSWORD_ERROR.equals(pcode)) {
			return outResult(pcode, "密码错误", null);
		}else {
			return outResult(pcode, "错误", null);
		}
		
		// 转账业务流程
		String code = "";
		try {
			code = tranflowService.transfer(ObjectToTransflow.objectToTranflow(inTransflow));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return outResult(RCodeDict.UPDATE_DATA_FAIL, "转账失败", null);
		}
		if(RCodeDict.SUCCESS.equals(code)) {
			return outResult(code, "转账成功", null);
		}else {
			if(RCodeDict.NOT_SUFFICIENT_FUNDS.equals(code)) {
				return outResult(code, "转账失败，余额不足!", null);
			}else {
				return outResult(code, "转账失败", null);
			}
		}	
	}
	
	/**
	 * 查询用户的所有账户
	 * @param userId
	 * @return
	 */
	@RequestMapping("listUseracct")
	@ResponseBody
	public Map<String, Object> listByUser(int userId){
		
		List<Useracct> list = tranflowService.findUseracctListByUserId(userId);
		if(list == null || list.isEmpty()) {
			return outResult(RCodeDict.DATA_NOT_FOUND, "未找到数据",null);
		}		
		return outResult(RCodeDict.SUCCESS, "数据获取完成", list);
	}
	
	
	
	/**
	 * 接口返回数据类型
	 */
	private Map<String, Object> outResult(String code, String msg, Object data){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("msg", msg);
		map.put("data", data);
		return map;
	}
	
	private boolean isEmpry(String str) {
		if(str == null || "".equals(str)) {
			return true;
		}
		return false;
	}
	
	private boolean isEnpry(Double data) {
		if(data == null) {
			return true;
		}
		return false;
	}	
}
